package org.accp.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import org.accp.dao.HisroleDao;
import org.accp.dao.HisuserDao;
import org.accp.dao.PermissionDao;
import org.accp.pojo.Hisrole;
import org.accp.pojo.Hisuser;
import org.accp.pojo.Permission;
import org.accp.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/user")
@CrossOrigin(value = "*")
public class HisuserController {
    @Autowired
    private HisuserDao hisuserDao;
    @Autowired
    private PermissionDao  permissionDao;
    @Autowired
    private HisroleDao hisroleDao;
    @RequestMapping("/search")
    @ResponseBody
    public List<Hisuser> search() throws Exception{
        return hisuserDao.getAll();
    }

    @RequestMapping("/Adminlogin")
    @ResponseBody
    public ResponseResult login(String username, String password) throws Exception{
        Hisuser user = hisuserDao.getByUserName(username);
        if (user == null){
            return new ResponseResult(1,"用户名或者密码错误");
        }
        if (!user.getPassword().equals(password)){
            return new ResponseResult(1,"用户名或者密码错误");
        }
        System.out.println(user.getUserid());
        List<Permission> permissions = hisuserDao.getPersByUserId(user.getUserid());
        Map<String,Object> map = new HashMap<>();
        map.put("user",user);

        map.put("user_pers",permissions);

        return new ResponseResult(map);
    }
    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult<List<Hisuser>> list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, Hisuser condition, String roleids) throws Exception {
        Map<String,Object> map = new HashMap<>();


        if(StringUtil.isNotEmpty(roleids)){
            String[] args = roleids.split(",");
            List<Hisrole> hisroles = new ArrayList<>();
            for (String str: args) {
                Hisrole hisrole = new Hisrole();
                hisrole.setRoleid(Integer.parseInt(str));
                hisroles.add(hisrole);
            }
            condition.setHisroles(hisroles);
        }

        Page<Hisuser> pager = PageHelper.startPage(page, limit);
        List<Hisuser> list = hisuserDao.getConditionList(condition);


        return new ResponseResult(pager.getTotal(),list);
    }
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(int userid) throws Exception{
        hisuserDao.delete(userid);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(Hisuser hisuser, String roleids ) throws Exception{
        hisuserDao.add(hisuser);
        //添加关系
        String[] array = roleids.split(",");
        int[] ids = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            ids[i] = Integer.parseInt(array[i]);
        }
        hisuserDao.addUserRole(hisuser.getUserid(),ids);
        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/update")
    @ResponseBody
    public ResponseResult update(Hisuser hisuser ,String roleids) throws Exception{
        hisuserDao.update(hisuser);
        permissionDao.delete(hisuser.getUserid());
        //添加关系
        String[] array = roleids.split(",");
        int[] ids = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            ids[i] = Integer.parseInt(array[i]);

        }
        hisuserDao.addUserRole(hisuser.getUserid(),ids);
        return ResponseResult.createSuccessResult();
    }




}
